import pyperclip
from typing import List


class LineNumberer:
    """对每组指定行添加递增编号及前后字符串"""

    def __init__(self, n: int, m: int, start: int = 1, step: int = 1, prefix: str = '', suffix: str = ''):
        """
        n: 每组行数
        m: 要修改的行号（从1开始）
        start: 起始数字
        step: 递增步长
        prefix: 添加在数字后的字符串
        suffix: 添加在末尾的字符串
        """
        self.n = n
        self.m = m
        self.start = start
        self.step = step
        self.prefix = prefix
        self.suffix = suffix

    def process_lines(self, lines: List[str]) -> List[str]:
        """处理多行文本"""
        count = 0  # 已处理组数
        for idx in range(len(lines)):
            if idx % self.n == self.m - 1:
                number = self.start + count * self.step
                lines[idx] = f"{number}{self.prefix}{lines[idx]}{self.suffix}"
                count += 1
        return lines

    @staticmethod
    def read_input(end_marker: str = "") -> List[str]:
        """从控制台读取多行文本，连续空行结束"""
        print("请输入多行文本，输入空行结束（按两次 Enter 结束）：")
        lines = []
        empty_count = 0
        while True:
            line = input()
            if line == "":
                empty_count += 1
                if empty_count >= 2:
                    break
            else:
                empty_count = 0
            lines.append(line)
        # 清除尾部空行
        while lines and lines[-1] == "":
            lines.pop()
        return lines

    def process_and_copy(self, lines: List[str]) -> str:
        """处理文本并复制到剪贴板"""
        processed_lines = self.process_lines(lines)
        result = "\n".join(processed_lines)
        return result


if __name__ == "__main__":
    lines = LineNumberer.read_input()

    n = int(input("请输入每组的行数 n："))
    m = int(input("请输入要修改的第 m 行（从1开始）："))
    start = int(input("请输入起始数字 i："))
    step = int(input("请输入递增步长 j："))
    prefix = input("请输入要添加在数字后的字符串 x：")
    suffix = input("请输入要添加在末尾的字符串 y：")

    numberer = LineNumberer(n=n, m=m, start=start, step=step, prefix=prefix, suffix=suffix)
    final_text = numberer.process_and_copy(lines)

    print("\n处理后的文本：\n")
    print(final_text)
    print("\n✅ 已复制到剪贴板！")
